package com.jl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jl.commons.base.BaseController;
import com.jl.commons.constant.Constants;
import com.jl.commons.constant.ErrorCode;
import com.jl.commons.constant.ErrorMessage;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.result.ResultData;
import com.jl.commons.utils.BASE64;
import com.jl.model.Menu;
import com.jl.model.Role;
import com.jl.service.IMenuService;
import com.jl.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 江剑
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    @GetMapping("/roleDataGrid")
    @RequiresPermissions("/role/roleDataGrid")
    public String roleList(){
        return "role_list";
    }


    @GetMapping("/add")
    @RequiresPermissions("/role/add")
    public String rolePage(ModelMap mp, Long id){
        mp.put("id",id);
        return "role_add";
    }

    @PostMapping("/add")
    @RequiresPermissions("/role/add")
    @ResponseBody
    public ResultData addMenu(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        Role role = JSON.parseObject(value,Role.class);

        //新增数据
        role.setCreateId(getUserId());
        role.setGmtCreate(new Date());
        boolean b = roleService.save(role);
        if(b){
            roleService.editRoleMenus(role.getId(),role.getAddMenus(),role.getDeleteMenus());
        }
        return success(role);
    }

    @GetMapping("/edit")
    @RequiresPermissions("/role/edit")
    public String editRolePage(ModelMap mp,Long id){
        mp.put("id",id);
        return "role_add";
    }

    @PostMapping("/edit")
    @RequiresPermissions("/role/edit")
    @ResponseBody
    public ResultData editRole(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        Role role = JSON.parseObject(value,Role.class);

        //更新权限信息
       if(roleService.editRoleMenus(role.getId(),role.getAddMenus(),role.getDeleteMenus())) {
           role.setGmtModified(new Date());
           role.setModifyId(getUserId());
           roleService.updateById(role);
       }
        return success(role);
    }


    @PostMapping("/delete")
    @RequiresPermissions("/role/delete")
    @ResponseBody
    public ResultData deletesRole(String json){
        String value = BASE64.decode(json);
        if(null == value){
            return fail(ErrorCode.BASE64_DECODER_ERROR, ErrorMessage.BASE64_DECODER_ERROR);
        }
        List<Long> ids = JSONArray.parseArray(value,Long.class);
        boolean b = roleService.deleteRoleByIds(ids);
        if(b){
            return success();
        }
        return fail();
    }

    @PostMapping("/dataGrid")
    @RequiresPermissions("/role/dataGrid")
    @ResponseBody
    public ResultData roleDataGrid(Long current,Long pageSize,String searchCol,String searchValue){
        if(current == null || pageSize == null){
            return fail(ErrorCode.PAGE_PARAM_ERROR, ErrorMessage.PAGE_PARAM_ERROR);
        }
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        //目前的查询只有单表
        if(StringUtils.isNotBlank(searchValue) && StringUtils.isNotBlank(searchCol)){
            if(searchCol.equals(Constants.ID)){
                queryWrapper.eq("t.id",searchValue);
            }else{
                queryWrapper.like("t."+searchCol,searchValue);
            }
        }
        PageDataInfo<Map<String,Object>> pageDataInfo =new PageDataInfo<>();
        pageDataInfo.setCurrent(current);
        pageDataInfo.setSize(pageSize);
        roleService.selectRoleDataGrid(pageDataInfo,queryWrapper);
        return success(pageDataInfo);
    }

    /**
     * 获得当前角色的所有菜单
     * @param roleId
     * @return
     */
    @PostMapping("/ignMenus")
    @ResponseBody
    public ResultData getRoleMenus(Long roleId){
        List<Menu> menus = menuService.selectMenuByRoleId(roleId);
        if(null != menus && !menus.isEmpty()){
            return success(menus);
        }
     //   return fail(ErrorCode.ROLE__MENU_EMPTY,ErrorMessage.ROLE__MENU_EMPTY);

        return success();
    }
}